package com.xbongbong.parent.condition.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.parent.abstracts.AbstractConditionFieldHandler;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.LinkConditionPoJo;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.PaasDateUtil;
import com.xbongbong.parent.pojo.RangeTimePoJo;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.parent.util.ConditionUtil;
import com.xbongbong.parent.util.RangeTimeUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 *
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/3 19:41
 * @since v1.0
 */
@Component
public class ConditionFormDateTimeHandlerImpl extends AbstractConditionFieldHandler {

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.DATETIME.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity,  FieldAttrEntity targetField,
                                       BusinessRuleInfoPojo businessRuleInfoPojo ) throws XbbException {
        return data.get(fieldAttrEntity.getAttr());
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterConditionsPoJo) throws XbbException {
        RangeTimePoJo rangeTimePoJo = RangeTimeUtil.getRangeTime(fieldAttrEntity, targetField, filterConditionsPoJo.getSymbol(),  data.getLong(fieldAttrEntity.getAttr()));
        if (Objects.isNull(rangeTimePoJo)) {
            return null;
        }
        filterConditionsPoJo.setValue(rangeTimePoJo.getList());
        filterConditionsPoJo.setSymbol(rangeTimePoJo.getSymbol());
        if (rangeTimePoJo.getList().size() == 1) {
            return rangeTimePoJo.getList().get(0);
        }
        return rangeTimePoJo.getList();
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        return filterCondition.getValue();
    }



    @Override
    public Object handlerRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        if (Objects.equals(fieldAttrEntity.getDateType(), DateTimeEnum.TIME.getType())
                && Objects.equals(targetField.getDateType(), DateTimeEnum.DATETIME.getType())) {
            // 「日期」和「日期时间」可以互相对比。「日期」和「日期时间」字段对比时，仅日期相等即可。
            long dateInt = Long.parseLong(value.toString());
            Long beforeDate = PaasDateUtil.getBeforeDayBreak(dateInt);
            Long endDate = PaasDateUtil.getDayLastSecond(dateInt);
            List<Object> dateList = getLinkTime(linkConditionPoJo, beforeDate, endDate);
            return dateList;
        } else if (Objects.equals(fieldAttrEntity.getDateType(), DateTimeEnum.DATETIME.getType())
                && Objects.equals(targetField.getDateType(), DateTimeEnum.TIME.getType())) {
            // 「日期时间」和「日期」字段对比时。「日期时间」字段的时间应该为00:00:00，才能相等
            long dateInt = Long.parseLong(value.toString());
            Long beforeDate = PaasDateUtil.getBeforeDayBreak(dateInt);
            return beforeDate;
        } else if (Objects.equals(fieldAttrEntity.getDateType(), DateTimeEnum.DATETIME.getType())
                && Objects.equals(targetField.getDateType(), DateTimeEnum.DATETIME.getType())) {
            // // 「日期时间」和「日期时间」可以互相对比。因为没有精确到00秒，所以只能检索00秒到59秒。
            long dateInt = Long.parseLong(value.toString());
            Long beforeDate = PaasDateUtil.getDateFirstSecond(dateInt);
            Long endDate = PaasDateUtil.getDateLastSecond(dateInt);
            List<Object> dateList = getLinkTime(linkConditionPoJo, beforeDate, endDate);
            return dateList;
        }
        return value;
    }

    private List<Object> getLinkTime(LinkConditionPoJo linkConditionPoJo, Long beforeDate, Long endDate) {
        List<Object> dateList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (Objects.equals(ConditionEnum.EQUAL.getSymbol(), linkConditionPoJo.getSymbol())) {
            linkConditionPoJo.setSymbol(ConditionEnum.RANGE.getSymbol());
            dateList.add(beforeDate);
            dateList.add(endDate);
        }else if (Objects.equals(ConditionEnum.LESSEQUAL.getSymbol(), linkConditionPoJo.getSymbol())
                || Objects.equals(ConditionEnum.LESSTHAN.getSymbol(), linkConditionPoJo.getSymbol())) {
            dateList.add(endDate);
        } else if (Objects.equals(ConditionEnum.GREATEREQUAL.getSymbol(), linkConditionPoJo.getSymbol())
                || Objects.equals(ConditionEnum.GREATERTHAN.getSymbol(), linkConditionPoJo.getSymbol())) {
            dateList.add(beforeDate);
        }else {
            dateList.add(beforeDate);
        }
        return dateList;
    }


    @Override
    public Object handlerBusinessRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, LinkConditionPoJo linkConditionPoJo, Object value, Long linkFormId, Integer linkBusinessType, Integer linkSaasMark) throws XbbException {
        if (Objects.equals(fieldAttrEntity.getDateType(), DateTimeEnum.TIME.getType())
                && Objects.equals(targetField.getDateType(), DateTimeEnum.DATETIME.getType())) {
            // 「日期」和「日期时间」可以互相对比。「日期」和「日期时间」字段对比时，仅日期相等即可。
            long dateInt = Long.parseLong(value.toString());
            Long beforeDate = PaasDateUtil.getBeforeDayBreak(dateInt);
            Long endDate = PaasDateUtil.getDayLastSecond(dateInt);
            List<Object> dateList = getLinkTime(linkConditionPoJo, beforeDate, endDate);
            return dateList;
        } else if (Objects.equals(fieldAttrEntity.getDateType(), DateTimeEnum.DATETIME.getType())
                && Objects.equals(targetField.getDateType(), DateTimeEnum.TIME.getType())) {
            // 「日期时间」和「日期」字段对比时。「日期时间」字段的时间应该为00:00:00，才能相等
            long dateInt = Long.parseLong(value.toString());
            Long beforeDate = PaasDateUtil.getBeforeDayBreak(dateInt);
            return beforeDate;
        } else if (Objects.equals(fieldAttrEntity.getDateType(), DateTimeEnum.DATETIME.getType())
                && Objects.equals(targetField.getDateType(), DateTimeEnum.DATETIME.getType())) {
            // // 「日期时间」和「日期时间」可以互相对比。因为没有精确到00秒，所以只能检索00秒到59秒。
            long dateInt = Long.parseLong(value.toString());
            Long beforeDate = PaasDateUtil.getDateFirstSecond(dateInt);
            Long endDate = PaasDateUtil.getDateLastSecond(dateInt);
            List<Object> dateList = getLinkTime(linkConditionPoJo, beforeDate, endDate);
            return dateList;
        }
        return value;
    }


    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        return ConditionUtil.getRangeTime(filterCondition.getSymbol(), filterCondition.getValue());
    }
}
